﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using NHibernate.Cfg;
using System.Web.Hosting;
using System.IO;
using NHibernate.Mapping.Attributes;
using VemTransporte.Models.Basica;
using NHibernate.Event;

namespace VemTransporte.Models.Persistencia
{
    public sealed class NHibernateHelper
    {
        /// <summary>
        /// Session factory for sub-classes.
        /// </summary>
        private static ISessionFactory _sessionFactory;


        /*public GenericNHibernateDAO()
        {
            

        }/**/

        public static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    var configuration = new Configuration();

                    string nhConfigPath = HostingEnvironment.MapPath("~/App_Data/hibernate.cfg.xml");
                    if (File.Exists(nhConfigPath))
                    {
                        configuration.Configure(nhConfigPath);
                    }

                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(Cidade))); //Class Mapping                   
                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(Bairro))); //Class Mapping
                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(Endereco))); //Class Mapping

                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(TipoTelefone))); //Class Mapping
                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(EmpresaTelefone))); //Class Mapping                                      
                    
                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(Usuario))); //Class Mapping
                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(Linha))); //Class Mapping
                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(Localizacao))); //Class Mapping
                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(Parada))); //Class Mapping

                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(UsuarioLinha))); //Class Mapping
                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(Veiculo))); //Class Mapping
                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(VeiculoParada))); //Class Mapping
                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(Horario))); //Class Mapping
                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(Empresa))); //Class Mapping/**/
                    configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(LinhaParada))); //Class Mapping/**/

                    /*configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(T).Assembly)); //Class Mapping/**/

                    //configuration.AddAssembly(typeof(T).Assembly); //XML Mapping

                    _sessionFactory = configuration.BuildSessionFactory();
                }
                return _sessionFactory;
            }
        }

        static ISession _session;

        private static object locker = new object();

        public static ISession Session
        {
            get
            {
                if ((_session == null) || (!_session.IsOpen))
                {
                    lock (locker)
                    {
                        if (_session == null)
                        {
                            _session = SessionFactory.OpenSession();
                        }
                    }
                }

                return _session;
            }
            set
            {
                _session = value;
            }
        }

        public static void CloseSession()
        {
            if (!_sessionFactory.IsClosed)
            {
                _session.Close();
                _session.Dispose();
                _session = null;
                _sessionFactory.Close();
                _sessionFactory.Dispose();
                _sessionFactory = null;
            }
        }

    }
}

/*Configuration.AddInputStream(NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(
System.Reflection.Assembly.GetExecutingAssembly()));/**/

/*Configuration.SetDefaultAssembly(typeof(T).Assembly.FullName)
    .SetDefaultNamespace(typeof(T).Namespace)
    .AddDirectory(new DirectoryInfo(HostingEnvironment.MapPath("~/App_Data/")));/**/

/*Configuration.AddInputStream(
HbmSerializer.Default.Serialize(typeof(T).Assembly));/**/

/*Configuration.AddAssembly(typeof(T).Assembly);/**/


/*Configuration.AddAssembly(Assembly.GetCallingAssembly());/**/
